package cn.chenc.framework.dynamic.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import java.util.ArrayList;
import java.util.List;

/**
 * 　@description: 多数据源配置
 * 　@author secret
 * 　@date 2021/7/21 22:28
 *
 */
public class DynamicDataSource extends AbstractRoutingDataSource {
    private static final ThreadLocal<String> contextHolder = new ThreadLocal();
    private static List<String> dataSourceList = new ArrayList();

    public DynamicDataSource() {
    }

    public static void setDataSourceType(String dataSourceType) {
        contextHolder.set(dataSourceType);
    }

    public static String getDataSourceType() {
        return contextHolder.get() == null ? DataSourceName.MASTER : (String)contextHolder.get();
    }

    public static void clearDataSourceType() {
        contextHolder.remove();
    }

    public static void saveDataSourceTypeName(String name) {
        dataSourceList.add(name);
    }

    public static boolean checkDataSourceType(String name) {
        return dataSourceList.contains(name);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return getDataSourceType();
    }
}
